<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\Users;
use Predis\Client;

class Sile
{
    public function __construct()
    {
        $this->predis = new Client('127.0.0.1:6379');
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function userSiteInfo()
    {
        $uid = input('uid',2);
        //参数验证
        if(!is_numeric($uid)){
            return json(['code'=>2000,'msg'=>'参数不正确','data'=>'']);
        }
        //获取用户信息
        $getUserInfo = Users::find($uid);
        if(!$getUserInfo){
            return json(['code'=>3000,'msg'=>'用户信息错误','data'=>'']);
        }
        //用户
        $userSiteInfo = $this->predis->hget('userSiteInfo','u_id'.$getUserInfo->id);
        //团长
        $regimentalSiteInfo = $this->predis->hget('regimentalSiteInfo','u_id'.$getUserInfo->id);
        //店铺
        $storeSiteInfo = $this->predis->hget('storeSiteInfo','u_id'.$getUserInfo->id);

        switch ($getUserInfo->type)
        {
            case '':
                //团长地址(自提点)
                die('请传入正确数据');
                break;
            case 0:
                //用户地址
                if(!empty($userSiteInfo)){
                    $siteInfo = $userSiteInfo;
                }else{
                    $userSiteInfo = ReceivingAddress::where('u_id',$getUserInfo->id)->select()->toArray();
                    $siteInfo = json_encode($userSiteInfo);
                    $this->predis->hset('userSiteInfo','u_id'.$getUserInfo->id,$userSiteInfo);
                }
                break;
            case 1:
                //团长地址(自提点)
                if(!empty($regimentalSiteInfo)){
                    $siteInfo = $regimentalSiteInfo;
                }else{
                    $userSiteInfo = SelfMention::where('u_id',$getUserInfo->id)->select()->toArray();
                    $siteInfo = json_encode($userSiteInfo);
                    $this->predis->hset('regimentalSiteInfo','u_id'.$getUserInfo->id,$userSiteInfo);
                }
                break;
            case 2:
                //店铺(门店)地址
                if(!empty($storeSiteInfo)){
                    $siteInfo = $storeSiteInfo;
                }else{
                    $userSiteInfo = SotreAddress::where('u_id',$getUserInfo->id)->select()->toArray();
                    $siteInfo = json_encode($userSiteInfo);
                    $this->predis->hset('storeSiteInfo','u_id'.$getUserInfo->id,$userSiteInfo);
                }
                break;
            default:
                echo "没有这个类型信息";
                break;
        }

        print_r($siteInfo);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function addSiteInfo()
    {
        $uid = input('uid',2);
        $data = input('data',['id'=>1,'uid'=>'2','name'=>'崔晏恺']);
        //参数验证
        if(!is_numeric($uid)){
            return json(['code'=>2000,'msg'=>'参数不正确','data'=>'']);
        }
        //获取用户信息
        $getUserInfo = Users::find($uid);
        if(!$getUserInfo){
            return json(['code'=>3000,'msg'=>'用户信息错误','data'=>'']);
        }
        //用户
        $userSiteInfo = $this->predis->hget('userSiteInfo','u_id'.$getUserInfo->id);
        //团长
        $regimentalSiteInfo = $this->predis->hget('regimentalSiteInfo','u_id'.$getUserInfo->id);
        //店铺
        $storeSiteInfo = $this->predis->hget('storeSiteInfo','u_id'.$getUserInfo->id);

        switch ($getUserInfo->type)
        {
            case '':
                //团长地址(自提点)
                die('请传入正确数据');
                break;
            case 0:
                //用户地址
                $userSiteInfo = json_decode($userSiteInfo);
                array_push($userSiteInfo,$data);
                $userSiteInfo = json_encode($userSiteInfo);
                $this->predis->hset('userSiteInfo','u_id'.$getUserInfo->id,$userSiteInfo);
                break;
            case 1:
                //团长地址(自提点)
                $regimentalSiteInfo = json_decode($regimentalSiteInfo);
                array_push($regimentalSiteInfo,$data);
                $regimentalSiteInfo = json_encode($regimentalSiteInfo);
                $this->predis->hset('regimentalSiteInfo','u_id'.$getUserInfo->id,$regimentalSiteInfo);
                break;
            case 2:
                //店铺(门店)地址
                $storeSiteInfo = json_decode($storeSiteInfo);
                array_push($storeSiteInfo,$data);
                $storeSiteInfo = json_encode($storeSiteInfo);
                $this->predis->hset('storeSiteInfo','u_id'.$getUserInfo->id,$storeSiteInfo);
                break;
            default:
                echo "没有这个类型信息";
                break;
        }

    }


}
